
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

class Request
{
public:
    Request()
    {
    }
    Request(int x, int y, char oper)
        : _x(x),
          _y(y),
          _oper(oper)
    {
    }
    // 序列化
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        // Json::Value sub;
        // sub["name"] = "zhangsan";
        // sub["age"] = 12;

        // {"oper":43,"sub":{"age":12,"name":"zhangsan"},"x":111,"y":222}
        // root["sub"] = sub;

         Json::FastWriter Writer;
        //Json::StyledWriter Writer;

        // 得到序列化之后的字符串
        // {"oper":43,"x":111,"y":222}
        std::string s = Writer.write(root);

        *out = s;
        return true;
    }
    // 反序列化
    bool Deserialize(const std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool res = reader.parse(in, root);

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();

        return true;
    }
    void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _y << std::endl;
        std::cout << _oper << std::endl;
    }
    ~Request()
    {
    }

private:
    int _x;
    int _y;
    char _oper; // + - * / %
};

int main()
{
    // Request req(111, 222, '+');
    // std::string s;
    // req.Serialize(&s);
    // std::cout << s << std::endl;

    std::string s = "{\"oper\":43,\"x\":111,\"y\":222}";
    Request req;
    req.Deserialize(s);
    req.Print();

    return 0;
}